using Microsoft.EntityFrameworkCore;
using JGSY.CourseManagement.Domain.Entities;
using System.IO;
using System;

namespace JGSY.CourseManagement.Infrastructure.Data.EntityFramework
{
    /// <summary>
    /// SQLite数据库上下文，用于管理用户会话数据
    /// </summary>
    public class SessionDbContext : DbContext
    {
        /// <summary>
        /// 初始化<see cref="SessionDbContext"/>类的新实例
        /// </summary>
        /// <param name="options">数据库上下文配置选项</param>
        public SessionDbContext(DbContextOptions<SessionDbContext> options) : base(options)
        { }

        /// <summary>
        /// 用户会话集合
        /// </summary>
        public DbSet<UserSession> UserSessions { get; set; }
        public DbSet<User> Users { get; set; }

        /// <summary>
        /// 配置实体映射和约束
        /// </summary>
        /// <param name="modelBuilder"></param>
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            // 配置UserSession实体
            modelBuilder.Entity<UserSession>(entity =>
            {
                //// 配置主键
                //entity.HasKey(e => e.Id);

                //// 确保Token唯一
                //entity.HasIndex(e => e.Token).IsUnique();

                // 配置与User的关系
                entity.HasOne<User>()
                    .WithMany()
                    .HasForeignKey(e => e.UserId)
                    .IsRequired();

                // 配置过期时间索引，加速会话清理
                entity.HasIndex(e => e.ExpireTime);

                // 配置字符串属性的最大长度和列类型
                entity.Property(e => e.Id)
                    .HasMaxLength(36)
                    .IsRequired();

                entity.Property(e => e.Token)
                    .HasMaxLength(100)
                    .IsRequired();

                entity.Property(e => e.UserId)
                    .HasMaxLength(50)
                    .IsRequired();

                entity.Property(e => e.UserName)
                    .HasMaxLength(50)
                    .IsRequired();

                entity.Property(e => e.RealName)
                    .HasMaxLength(50);

                entity.Property(e => e.Password)
                    .HasMaxLength(100);

                entity.Property(e => e.Avatar)
                    .HasMaxLength(200);

                // 配置数值和日期属性
                entity.Property(e => e.Gender)
                    .HasDefaultValue(0);

                entity.Property(e => e.LoginTime)
                    .IsRequired();

                entity.Property(e => e.ExpireTime)
                    .IsRequired();

                entity.Property(e => e.CreationTime)
                    .IsRequired();
            });

            // 配置User实体
            modelBuilder.Entity<User>(entity =>
            {
                entity.HasKey(e => e.UserId);

                // 确保用户名唯一
                entity.HasIndex(e => e.UserName).IsUnique();

                // 配置其他属性
                entity.Property(e => e.UserId)
                    .HasMaxLength(50)
                    .IsRequired();

                entity.Property(e => e.UserName)
                    .HasMaxLength(50)
                    .IsRequired();
            });
        }
    }
}